package com.zhitong.money.controller;

import com.github.pagehelper.PageInfo;
import com.zhitong.money.entity.AjaxResponse;
import com.zhitong.money.entity.PSalaryParam;
import com.zhitong.money.exception.RepeatException;
import com.zhitong.money.services.api.IPSalaryParamService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@Slf4j
@RestController
public class PSalaryParamContro {

    @Autowired
    private IPSalaryParamService service;

    @GetMapping("/params")
    public AjaxResponse getAllPSalaryParam()  throws Exception{
        List<PSalaryParam> allPSalaryParam = service.findAllPSalaryParam();
        return AjaxResponse.success(allPSalaryParam);
    }

    @PostMapping("/params")
    public AjaxResponse getAllParamsByPage(@RequestBody Map<String,Object> map) throws Exception{
        PageInfo allPSalaryParamByPage = service.findAllPSalaryParamByPage(map);
        return AjaxResponse.success(allPSalaryParamByPage);
    }

    @GetMapping("/param/{id}")
    public AjaxResponse getParamById(@PathVariable String id)throws Exception{
        PSalaryParam pSalaryParamById = service.findPSalaryParamById(id);
        return AjaxResponse.success(pSalaryParamById);
    }

    @GetMapping("/paramPid/{pid}")
    public AjaxResponse getParamByPid(@PathVariable String pid)throws Exception{
        PSalaryParam pSalaryParamById = service.findParamByPid(pid);
        return AjaxResponse.success(pSalaryParamById);
    }

    @RequiresPermissions({"addParam"})
    @PostMapping("/param")
    public AjaxResponse addParam(@RequestBody PSalaryParam pSalaryParam) throws Exception{
        service.addPSalaryParam(pSalaryParam);
        return AjaxResponse.success("添加工资参数成功!");
    }

    @RequiresPermissions({"updateParam"})
    @PutMapping("/param")
    public AjaxResponse updateParam(@RequestBody PSalaryParam pSalaryParam) throws Exception{
        service.updatePSalaryParam(pSalaryParam);
        return AjaxResponse.success("修改工资参数成功!");
    }

    @RequiresPermissions({"delParam"})
    @DeleteMapping("/param/{id}")
    public AjaxResponse delParam(@PathVariable("id") String id) throws Exception{
        service.delPSalaryParam(id);
        return AjaxResponse.success("删除工资参数成功!");
    }

    @RequiresPermissions({"delParam"})
    @PostMapping("/delparams")
    public AjaxResponse delParamByIds(@RequestBody List<String> ids) throws Exception{
        service.delPSalaryByIds(ids);
        return AjaxResponse.success("删除工资参数成功!");
    }

}
